package com.mall.search.controller;

import com.common.dto.es.SkuESModel;
import com.common.exception.BizCodeEnums;
import com.common.utils.R;
import com.mall.search.service.ElasticSearchSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RequestMapping("/search/save")
@RestController
@Slf4j
public class ElasticSearchSaveController {
    @Autowired
    private ElasticSearchSaveService elasticSearchSaveService;
    @PostMapping("/product")
    public R productStatusUp(@RequestBody List<SkuESModel> skuESModels) throws IOException {
        boolean flag = false;
        try {
            flag = elasticSearchSaveService.productStatusUp(skuESModels);
        } catch (IOException e) {
            log.error("商品上架失败",e);
            return R.error(BizCodeEnums.PRODUCT_UP_EXCEPTION.getCode(),BizCodeEnums.PRODUCT_UP_EXCEPTION.getMsg());
        }
        if (flag){
            return R.ok();
        }
        return R.error(BizCodeEnums.PRODUCT_UP_EXCEPTION.getCode(),BizCodeEnums.PRODUCT_UP_EXCEPTION.getMsg());
    }
}
